import axios from "axios";
// import { getToken } from "./token";
import router from "../router";
import { getToken } from "./token";
const request = axios.create({
  baseURL: "http://geek.itheima.net/v1_0",
  timeout: 5000,
});

request.interceptors.request.use(
  (config) => {
    const token = getToken();
    // const token =localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.log(error);
   
    if (error.response.status=== 401) {
   localStorage.clear('token')
      router.navigate('/login')
      window.location.reload()
    }
    return Promise.reject(error);
  }
);

export { request };
